<template>
  <div>
    <el-card shadow="always" style="margin-bottom: 5px">
      <div class="set-title">IO设置</div>
    </el-card>
    <el-card shadow="always" style="margin-bottom: 5px">
      <div class="set-title">量仪类型</div>
      <el-form size="small" label-width="80px">
        <el-form-item label="当前类型">
          <el-select size="small" v-model="config.frame">
            <el-option
              v-for="item in measureTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="always" style="margin-bottom: 5px">
      <div class="set-title">串口设置</div>
      <el-row :gutter="5">
        <el-form size="small" label-width="80px">
          <el-form-item label="当前串口:">
            <el-select
              v-model="config.serial.path"
              clearable
              size="small"
              placeholder="COM1,COM2,COM3..."
            >
              <el-option
                v-for="item in serials"
                :key="item.path"
                :label="item.path"
                :value="item.path"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="波特率:">
            <el-select size="small" v-model="config.serial.baudrate">
              <el-option
                v-for="(item, i) in baudrates"
                :key="i"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数据位:">
            <el-select size="small" v-model="config.serial.databit">
              <el-option
                v-for="(item, i) in databits"
                :key="i"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="停止位:">
            <el-select size="small" v-model="config.serial.stopbit">
              <el-option
                v-for="(item, i) in stopbits"
                :key="i"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="校验:">
            <el-select size="small" v-model="config.serial.parity">
              <el-option
                v-for="(item, i) in paritys"
                :key="i"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </el-row>
    </el-card>
    <el-button @click="updateConfig">应用</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      config: {
        addPort: "",
        triggerPort: "",
        deletePort: "",
        frame: "",
        serial: {
          path: "",
          baudrate: "",
          databit: "",
          stopbit: "",
          parity: "",
        },
      },
      serials: [],
      baudrates: [4800, 9600, 19200, 38400],
      databits: [7, 8],
      stopbits: [0, 1, 2],
      paritys: ["none", "奇校验", "偶校验"],
      measureTypes: [
        { value: "normal", label: "通用" },
        { value: "vat", label: "气缸" },
        { value: "roller", label: "滚子" },
        { value: "crank", label: "曲轴" },
        { value: "spc", label: "SPC" },
      ],
    };
  },
  created() {
    this.getConfigInfo();
    this.getSerials();
  },
  methods: {
    getSerials() {
      this.$http({
        method: "get",
        url: "/system/serials",
        params: {},
        headers: {},
      }).then((res) => {
        this.serials = res.data;
        // console.log(this.serials);
      });
    },
    getConfigInfo() {
      this.$http({
        method: "get",
        url: "/system/config",
        params: {},
        headers: {},
      }).then((res) => {
        var config = res.data;
        this.config.addPort = config.addPort;
        this.config.deletePort = config.deletePort;
        this.config.triggerPort = config.triggerPort;
        this.config.frame = config.frame;
        this.config.serial.path = config.serialPort.serialPath;
        this.config.serial.baudrate = config.serialPort.baudRate;
        this.config.serial.databit = config.serialPort.dataBits;
        this.config.serial.stopbit = config.serialPort.stopBits;
        this.config.serial.parity = config.serialPort.parity;
        // console.log(config);
      });
    },
    updateConfig() {
    
      this.$http({
        method: "put",
        url: "/system/config",
        params: {},
        data:{
            addPort:this.config.addPort,
            deletePort:this.config.deletePort,
            triggerPort:this.config.triggerPort,
            frame:this.config.frame,
            serialPath:this.config.serial.path
        },
        headers: {
            "Content-Type": "application/json;charset=UTF-8"
        },
      }).then((res) => {
        // this.serials = res.data;
        console.log(res.data);
      });
    },
  },
};
</script>

<style lang="sass" scoped>
@import url('../assets/style/hardware.scss')
</style>